package serve.cnf;

import serve.controller.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterMapping {

    public static Map<String, Cnf> URLMapping = new HashMap<String, Cnf>();

    /**
     * 注册动态路由.....
     *
     * @param pattern    路径
     * @param controller 响应类名
     * @throws Exception 如果重复注册同一个路由会抛出异常
     */
    public static void register(String pattern, Controller controller) throws Exception {



        // 处理路由的参数问题
        String[] parts = pattern.split("/");

        List<String> params = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < parts.length; i++) {
            String s = parts[i];
            System.out.println(s);

            if (s.startsWith(":")) {
                System.out.println("starts with " + s);

                int index = s.indexOf("(");
                //  System.out.println("index is " + index + "sub " + s.substring(0, index) + " sub " + s.substring(index, s.length()));
                params.add(j, s.substring(0, index));
                j++;
                // 重新设置 pattern
                parts[i] = s.substring(index);
            }
        }
        String join = String.join("/", parts);
        // 判断路由是否已经存在了, 如果该路由已经存在了,程序直接崩溃就行了
        if (URLMapping.containsKey(join)) {
            throw new Exception("could not re-register url pattern " + join);
        }
        System.out.println("join " + join);
        System.out.println("params " + params);


        Cnf cnf = new Cnf();
        cnf.pattern = Pattern.compile(join);
        cnf.params = params;
        cnf.clazz = controller.getClass();
        cnf.classPath = "";

        // 将参数添加到路由匹配表
        URLMapping.put(join, cnf);

    }


    private static void dispatcher(String url) {

        Map<String, String> params = new HashMap<>();
        // 进行正则匹配
        for (Map.Entry<String, Cnf> mp : URLMapping.entrySet()) {

            Cnf cnf = mp.getValue();
            Matcher matcher = cnf.pattern.matcher(url);
            if (!matcher.find()) {
                // System.out.println("无法匹配..."+ url);
                continue;
            }

            if (!matcher.group().equals(url)) {
                //  System.out.println("pass "+" 长度不匹配 "+ url);
                continue;
            }

            System.out.println("长度匹配, " + matcher.group() + "    url is " + url + " count  " + matcher.groupCount());


            for (int i = 0; i < matcher.groupCount(); i++) {
                System.out.println("find " + i + " value " + matcher.group(i + 1));
                params.put(cnf.params.get(i), matcher.group(i + 1));
            }
            // System.out.println("f*ck " + matcher.group(2) + " count " + matcher.groupCount() + params);
        }


    }


    public static void main(String[] args) {
        try {
            register("/index/:uid([0-9]+)/query",new  Controller());
            register("/index/:uid([0-9]+)/query/hs", new  Controller());
            register("/index/:name([a-z|A-Z]+)/:uid([0-9]+)/query",new Controller());
            dispatcher("/index/123/query/index/123/query");
            dispatcher("/index/123/query");
            dispatcher("/index/abc/2333/query");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
